function Property(name, value, validator, unique, currentId, showResultText, errorStyle, bindingPath, relatedProperty) {
    this.name = name;
    this.value = value;
    this.validator = validator;
    this.unique = unique;
    this.currentId = currentId;
    this.showResultText = showResultText;
    this.errorStyle = errorStyle;
    this.bindingPath = bindingPath;
    this.relatedProperty = relatedProperty;
}

function validateAll(form, event) {
    var length = form.elements.length;
    var properties = new Array();
    var index = 0;
    var submitButton;
    for (var i = 0; i < length; i++) {
        var element = form.elements[i];
        if (element.getAttribute('type') == 'submit') {
            submitButton = element;
        }
        if (element.getAttribute('needvalidation') == 'true') {
            var propertyName = element.getAttribute('propertyname');
            var propertyValue = element.value != element.getAttribute('alt') ? element.value : "";
            var validatorName = element.getAttribute('validatorname');
            var unique = element.getAttribute('unique');
            var showResultText = element.getAttribute('showresulttext');
            var errorStyle = element.getAttribute('errorstyle');
            var bindingPath = element.getAttribute('bindingpath');
            var relatedPropertyId = element.getAttribute('relatedProperty');
            var relatedProperty = null;
            if (relatedPropertyId != null && relatedPropertyId != "") {
                relatedProperty = document.getElementById(relatedPropertyId).value;
            }
            if (unique == 'false') {
                properties[index] = new Property(propertyName, propertyValue, validatorName, false, 0, showResultText, errorStyle, bindingPath, relatedProperty);
            } else {
                var currentId = element.getAttribute('currentid');
                properties[index] = new Property(propertyName, propertyValue, validatorName, true, currentId, showResultText, errorStyle, bindingPath, relatedProperty);
            }
            index = index + 1;
        }
        //validate the comfirm same
        if (element.getAttribute('confirmPassword') != null) {
            var passwordId = element.getAttribute('confirmPassword');
            var password = document.getElementById(passwordId).value;
            properties[index] = new Property('confirmPassword', element.value, "defaultValidator", false, 0, true, "null", password);
            index = index + 1;
        }
    }
    submitButton.disabled = 'disabled';
    remoteValidateAll(properties, event, form, submitButton);
    return false;
}

function validate(propertyName, propertyValue, validatorName, unique, currentId, showResultText, errorStyle, bindingPath, relatedProperty) {
    var errorSpanId = validatorName + '-' + bindingPath + 'Warning';
    document.getElementById(errorSpanId).innerHTML = '<img src="/images/icons/indicator.gif" border="0"/>';

    var relatedPropertyValue;
    if (relatedProperty == "null") {
        relatedPropertyValue = null;
    } else {
        relatedPropertyValue = document.getElementById(relatedProperty).value;
    }
    var properties = new Array();
    properties[0] = new Property(propertyName, propertyValue, validatorName, unique, currentId, showResultText, errorStyle, bindingPath, relatedPropertyValue);
    remoteValidate(properties);
}
function remoteValidate(properties) {
    ValidatorRegistrar.validate(properties, function(validationResult) {
        insertInnerHtml(validationResult.validationMessages[0])
    });
}

function insertInnerHtml(result) {
    var property = result.property;
    var valid = result.valid;
    var bindingPath = property.bindingPath;
    var errorStyle = property.errorStyle;
    var bindingElement = document.getElementById(bindingPath);
    if (!valid) {
        var originalStyle = $(bindingElement).attr("style");
        if (originalStyle != null) {
            errorStyle = originalStyle + ";" + errorStyle;
        }
        $(bindingElement).attr("style", errorStyle);
    } else {
        var customerCssStyle = $(bindingElement).attr("customercssstyle");
        if (customerCssStyle != null) {
            $(bindingElement).attr("style", customerCssStyle);
        }
    }
    if (property.showResultText) {
        var msg = result.message;
        var validator = result.property.validator;
        var propertyName = result.property.bindingPath;
        if (property.name == "confirmPassword") {
            propertyName = "confirmPassword";
        }
        var errorSpanId = validator + '-' + propertyName + 'Warning';
        document.getElementById(errorSpanId).innerHTML = msg;
    }
}

function remoteValidateAll(properties, event, form, submitButton) {
    ValidatorRegistrar.validate(properties, function(validationResult) {
        handleRemoteMethod(validationResult, event, form, submitButton);
    });
}


function handleRemoteMethod(validationResult, event, form, submitButton) {
    var hasError = false;
    var results = validationResult.validationMessages;
    for (var i = 0; i < results.length; i++) {
        var valid = results[i].valid;
        insertInnerHtml(results[i]);
        if (!valid) {
            hasError = true;
        }
    }
    if (hasError) {
        submitButton.disabled = "";
        try {
            if (event.preventDefault != undefined) {
                event.preventDefault();
            } else if (event.returnValue) {
                event.returnValue = false;
            }
        } catch(e) {
        }
    } else {
        if (preSubmit()) {
            form.submit();
        } else {
            submitButton.disabled = "";
        }
    }
}
function preSubmit() {
    //Default doing nothing
    var vcElement = document.getElementById("verifycode");
    if (null != vcElement && (vcElement.value.isEmpty() || vcElement.value.equals(vcElement.getAttribute("alt")))) {
        vcElement.focus();
        alert("请输入验证码！");
        return false;
    }
    return true;
}
